package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/13 16:04
 */
//提交预约请求
@RestController
@RequestMapping("/order")
public class OrderController {
    //注入预约服务service
    @Reference
    private OrderService orderService;
    //注入Redis缓存
    @Autowired
    private JedisPool jedisPool;



    /*
    移动端-->根据id查询预约信息(套餐信息,会员信息)
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        //程序健壮性和安全性处理
        try {
            //查询预约信息
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /*
    移动端-->体检预约提交
     */
    @RequestMapping("/submit")
    public Result submitOrder(@RequestBody Map map){
        //获取手机号
        String telephone = (String) map.get("telephone");
        //从Redis中获取缓存的验证码,key为手机号+ RedisConstant.SENDTYPE_ORDER
        String codeInRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //获取用户输入的验证码
        String validateCode = (String) map.get("validateCode");

        //判断验证码是否正确
        if(codeInRedis == null || !codeInRedis.equals(validateCode)){//校验验证码失败
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);

        }else{//校验验证码通过
            Result result = null;
            //程序健壮性和安全性处理:避免网络异常抛出异常
            //调用体检预约服务,
            try {
                //设置预约类型:微信预约,电话预约
                map.put("orderType", Order.ORDERTYPE_WEIXIN);
                //通过dubbo远程调用服务实现在线预约业务处理
                result = orderService.order(map);
            } catch (Exception e) {
                e.printStackTrace();
                //如果为null,服务没有调通,提示请重试
                 if(null == result){
                    return new Result(false,MessageConstant.ORDER_FAIL);
                 }
                return result;
            }

            if(result.isFlag()){//预约成功,发送短信通知
                String orderDate = (String) map.get("orderDate");
                try {
                    //发送短信ClientExcepti
                    //SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,orderDate);
                    System.out.println("随机生成的验证码:" + codeInRedis);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }

    }

}
